// 退货原因类型
var returnReasonTypeTable;

$(document).ready(function () {
    
    $.validator.addMethod("checkLetter", function(value, element) {
        var reg = /^[0-9a-zA-Z\u4E00-\u9FA5\r\n\s/.]*$/; 
        if (!reg.test(value)) {
            return false;
        }
        
        return true;
    }, "请输入数字，大小写字母，汉字");
    
    $("#newForm").validate({
        debug:true,
        errorPlacement: function(error, element)
        {
            error.insertAfter(element);
        }
    });

    $("#editForm").validate({
        debug:true,
        errorPlacement: function(error, element)
        {
            error.insertAfter(element);
        }
    });

    // 退货原因类型list
    returnReasonTypeTable = $('#tblReturnReason').DataTable({
        "processing": true,
        "serverSide": true,
        "lengthMenu" : [ [ 10, 25, 50, 100, 500, 1000 ], [ 10, 25, 50, 100, 500, 1000] ],
        "dom": '<"bottom"rtflp>',
        "searching": false,
        "pagingType": "full_numbers",
        "deferRender": true,
        "ajax": {
            "url": "/returnReasonType/getReturnReasonTypeList.action",
            "data": function ( d ) {
                d.quickSearch = encodeURI($('#quickSearch').val());
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "returnReasonCode", "searchable": false},
            {"data": "returnReasonName", "searchable": false},
            {"data": "remark", "searchable": false},
            {"data": "operation", "searchable": false, "orderable": false}
        ],
        "columnDefs": [
            {"className": "truncate-text tooltip-column-detail", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail", "targets":[2]},
            {"targets":[3]}
        ],
        "order": [[ 0, "asc" ]]
    });
    
    // 文字过长截断显示悬浮框
    $("#tblReturnReason").tooltip({
        selector: ".tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
    
    // 快速搜索
    $("#btnQuickSearch").click(function () {
        returnReasonTypeTable.draw();
    });

    // 搜索框的回车事件
    $('#quickSearch').keydown(function(e){
        if(e.keyCode==13){
            $("#btnQuickSearch").click();
        }
    }); 

    $('#frmNewReturnReasonType').on('hidden.bs.modal', function () {
        $("#newForm")[0].reset();
    });
    
    $('#frmNewReturnReasonType').on('hide.bs.modal', function () {
        window.top.window.returnCustomModalDialog();
        $("#newForm").validate().clear();
    });
    
    $('#frmModifyReturnReasonType').on('hidden.bs.modal', function () {
        $("#editForm")[0].reset();
    });

    $('#frmModifyReturnReasonType').on('hide.bs.modal', function () {
        window.top.window.returnCustomModalDialog();
        $("#editForm").validate().clear();
    })
    
});

// 弹出新增退货原因类型对话框
$("#newReturnReasonTypeHref").click(function () {
    var editDialog = window.top.window.borrowCustomModalDialog($("#frmNewReturnReasonType"));
    editDialog.modal({show:true, backdrop:'static'});
});

// 保存新增退货原因类型
$("#btnSave").click(function () {
    window.top.window.returnCustomModalDialog();

    if(!$("#newForm").valid()) {
        window.top.window.borrowCustomModalDialog($("#frmNewReturnReasonType"));
        return false;
    }

    var returnReasonCode = $('#returnReasonCode').val();
    var returnReasonName = $('#returnReasonName').val();
    var remark = $('#remark').val();
    var data = {
        "returnReasonCode": returnReasonCode,
        "returnReasonName": returnReasonName,
        "remark": remark
    }
    
    var newForm=window.top.window.borrowCustomModalDialog($("#frmNewReturnReasonType"));

    var loading = window.top.window.getIndexBusyOverlay();
    $.ajaxSettings['contentType'] = "application/x-www-form-urlencoded; charset=utf-8";
    $.ajax({
        type: "POST",
        url: "/returnReasonType/insertReturnReasonType.action",
        data: JSON.stringify(data),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
        success: function (json) {
        	loading.remove();
            if (json.result=="success") {
                newForm.modal('hide');
                $('#returnReasonCode').val('');
                $('#returnReasonName').val('');
                $('#remark').val('');
                window.top.window.showScoMessage('ok', '添加成功');
                returnReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
});

// 弹出修改退货原因类型对话框
function operationEdit(btnEdit) {
    var returnReasonId = $(btnEdit).attr("returnReasonId");
    $.ajax({
        type: "POST",
        url: "/returnReasonType/getReturnReasonTypeInfo/" + returnReasonId + ".action",
        data: null,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                $('#returnReasonId_m').val(returnReasonId);
                $('#returnReasonCode_m').val(data.data.returnReasonCode);
                $('#returnReasonName_m').val(data.data.returnReasonName);
                $('#remark_m').val(data.data.remark);
                var editDialog = window.top.window.borrowCustomModalDialog($("#frmModifyReturnReasonType"));
                editDialog.modal({show:true, backdrop:'static'});
            }
        }
    });

    $('#frmModifyReturnReasonType').modal('show');
}

// 保存修改退货原因类型
$("#btnSave_m").click(function () {
    window.top.window.returnCustomModalDialog();

    if(!$("#editForm").valid()) {
        window.top.window.borrowCustomModalDialog($("#frmModifyReturnReasonType"));
        return false;
    }

    var returnReasonId = $('#returnReasonId_m').val();
    var returnReasonCode = $('#returnReasonCode_m').val();
    var returnReasonName = $('#returnReasonName_m').val();
    var remark = $('#remark_m').val();
    var data = {
        "returnReasonId": returnReasonId,
        "returnReasonCode": returnReasonCode,
        "returnReasonName": returnReasonName,
        "remark": remark
    }

    var edit=window.top.window.borrowCustomModalDialog($("#frmModifyReturnReasonType"));

    var loading = window.top.window.getIndexBusyOverlay();
    $.ajaxSettings['contentType'] = "application/x-www-form-urlencoded; charset=utf-8";
    $.ajax({
        type: "POST",
        url: "/returnReasonType/updateReturnReasonType.action",
        data: JSON.stringify(data),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
        success: function (json) {
        	loading.remove();
            if (json.result == "true") {
                edit.modal('hide');
                $('#returnReasonCode_m').val('');
                $('#returnReasonName_m').val('');
                $('#remark_m').val('');
                window.top.window.showScoMessage('ok', '保存成功');
                returnReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
});

// 删除退货原因类型
function operationDelete(btnDelete) {
    var returnReasonId = $(btnDelete).attr("returnReasonId");
    $('#return_reason_id_delete').val(returnReasonId);
    window.top.window.showModalConfirm( "是否确认删除？", doDelete);
}

// 删除退货原因类型
function doDelete() {
    var returnReasonId = $('#return_reason_id_delete').val();

    var loading = window.top.window.getIndexBusyOverlay();
    $.ajax({
        type: "POST",
        url: "/returnReasonType/deleteReturnReasonType/" + returnReasonId + ".action",
        data: null,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
        success: function (json) {
        	loading.remove();
            if (json.success) {
            	loading.remove();
                window.top.window.showScoMessage('ok', '删除成功');
                returnReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
}
